package com.example.contestsystem.Controller;

import com.example.contestsystem.Entity.Contest;
import com.example.contestsystem.Entity.Teacher;
import com.example.contestsystem.R.R;
import com.example.contestsystem.Service.ContestService;
import com.example.contestsystem.Service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    final TeacherService teacherService;
    final ContestService contestService;

    public TeacherController(TeacherService teacherService, ContestService contestService){
        this.teacherService = teacherService;
        this.contestService = contestService;
    }

    @GetMapping("/login")
    public String login(){
        return "teacher/login";
    }

    @PostMapping("register")
    @ResponseBody
    public R register(@ModelAttribute Teacher teacher, HttpServletRequest httpServletRequest){
        if(this.teacherService.register(teacher) != null){
            httpServletRequest.getSession().setAttribute("loginTeacher", teacher);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/login")
    @ResponseBody
    public R login(@ModelAttribute Teacher teacher, HttpServletRequest httpServletRequest){
        if(this.teacherService.login(teacher)){
            // TODO: 2023/12/24 此代码可能太耗性能 
            teacher.setTeacherName(this.teacherService.findByTeacherAccount(teacher.getTeacherAccount()).getTeacherName());
            httpServletRequest.getSession().setAttribute("loginTeacher", teacher);
            httpServletRequest.getSession().setAttribute("loginStu", null);
            return R.ok(200,"登录成功");
        }else {
            return R.error();
        }
    }

    @PostMapping("/modify")
    @ResponseBody
    public String modify(@ModelAttribute Teacher teacher, HttpServletRequest httpServletRequest){
        Teacher loginTeacher = (Teacher)httpServletRequest.getSession().getAttribute("loginTeacher");
        if(teacher.getTeacherName() != null  && !teacher.getTeacherName().isEmpty()){
            loginTeacher.setTeacherName(teacher.getTeacherName());
        }
        if(teacher.getTeacherPassword() != null && !teacher.getTeacherPassword().isEmpty()){
            loginTeacher.setTeacherPassword(teacher.getTeacherPassword());
        }
        this.teacherService.modify(loginTeacher);
        httpServletRequest.getSession().setAttribute("loginTeacher", loginTeacher);
        return "teacher/profile";
    }

    /**
     * 
     */
    @GetMapping("/profile")
    public String profile(HttpServletRequest httpServletRequest) {
        Teacher loginTeacher = (Teacher) httpServletRequest.getSession().getAttribute("loginTeacher");
        if (loginTeacher != null) {
            return "teacher/information";
        }
        return "teacher/login";
    }

    @GetMapping("/get_teacher")
    @ResponseBody
    public Object getStu(HttpServletRequest httpServletRequest){
        Teacher loginteacher = (Teacher) httpServletRequest.getSession().getAttribute("loginTeacher");
        Teacher teacher = teacherService.findByTeacherAccount(loginteacher.getTeacherAccount());
        if(loginteacher != null ){
            return R.ok("成功",teacher,200);
        }
        return R.error(-1,"您未登录");
    }

    @GetMapping("/register")
    public String register(){
        return "stu/register";
    }

    @GetMapping("/publishedContest")
    public String contestList(HttpServletRequest httpServletRequest, Model model){
        Teacher loginTeacher = (Teacher)httpServletRequest.getSession().getAttribute("loginTeacher");
        if(loginTeacher!=null) {
            Page<Contest> contests = this.contestService.findByTeacherAccount(loginTeacher.getTeacherAccount(), 0, 20);
            int prePage = contests.previousOrFirstPageable().getPageNumber();
            int nextPage = contests.nextOrLastPageable().getPageNumber();
            model.addAttribute("contests", contests);
            model.addAttribute("prePage", prePage);
            model.addAttribute("nextPage", nextPage);
            return "teacher/contest_list";
        }
        return "teacher/login";
    }

    @GetMapping("/verifyTeacher")
    @ResponseBody
    public String verifyTeacher(@Param("teacherAccount") int teacherAccount){
        if(this.teacherService.verifyTeacher(teacherAccount))
            return "true";
        return "false";
    }

    @GetMapping("/publishContest")
    public String publish(HttpServletRequest httpServletRequest) {
        Teacher loginTeacher = (Teacher) httpServletRequest.getSession().getAttribute("loginTeacher");
        if (loginTeacher != null) {
            return "teacher/contest_create";
        }
        return "teacher/login";
    }
}
